/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fh.view.tourney;

import fh.view.running.CurrentTourneyOverview;
import fh.component.DateParser;
import fh.controller.EventController;
import fh.controller.LocationController;
import fh.model.Event;
import fh.model.Location;
import fh.view.*;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

/**
 *
 * @author Bart
 */
public class EditTourney extends javax.swing.JFrame {

    LocationController lc;
    Event anEvent;
    EventController ec;

    /**
     * Creates new form EditTourney
     */
    public EditTourney() {
        initComponents();
        lc = new LocationController();
        FillList();
    }

    public EditTourney(Event event) {
        initComponents();
        lc = new LocationController();
        anEvent = event;
        ec = new EventController();
        FillList();
        FillForm();
    }

    private void FillForm() {
        NameTourneyField.setText(anEvent.getName());
        DateTourneyField.setText(DateParser.mySqlDateToString(anEvent.getDate()));
        MaxPlayersTourneyField.setText(anEvent.getMax_players() + "");
        EntryFeeTourneyField.setText(anEvent.getEntry_fee() + "");
        TableCapTourneyField.setText(anEvent.getTable_cap() + "");
        Location currentLocation = lc.getLocationById(anEvent.getLocation_id());
        lblCurrentLocation.setText(currentLocation.toString());

    }

    private void FillList() {
        DefaultComboBoxModel locations = lc.getLocationsList();
        LocationList.clearSelection();
        LocationList.setModel(locations);
    }

    private int getNewLocation() {

        Location location = (Location) LocationList.getSelectedValue();
        if (location == null) {
            return anEvent.getLocation_id();
        } else {
            return location.getId();
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        CurrentTourneyButton = new javax.swing.JButton();
        LocationEventMainButton = new javax.swing.JButton();
        PlayerOverviewButton = new javax.swing.JButton();
        EditTourneyButton = new javax.swing.JButton();
        CancelEditTourneyButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        LocationList = new javax.swing.JList();
        TableCapTourneyField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        EntryFeeTourneyField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        MaxPlayersTourneyField = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        DateTourneyField = new javax.swing.JTextField();
        NameTourneyField = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        lblCurrentLocation = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Wijzigen toernooi");
        setMaximumSize(new java.awt.Dimension(800, 600));
        setMinimumSize(new java.awt.Dimension(800, 600));
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 30)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Full House");
        jLabel1.setToolTipText("");

        CurrentTourneyButton.setText("Toernooi runnen");
        CurrentTourneyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CurrentTourneyButtonActionPerformed(evt);
            }
        });

        LocationEventMainButton.setText("Locatie & Event");
        LocationEventMainButton.setMaximumSize(new java.awt.Dimension(111, 23));
        LocationEventMainButton.setMinimumSize(new java.awt.Dimension(111, 23));
        LocationEventMainButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LocationEventMainButtonActionPerformed(evt);
            }
        });

        PlayerOverviewButton.setText("Speler beheer");
        PlayerOverviewButton.setMaximumSize(new java.awt.Dimension(111, 23));
        PlayerOverviewButton.setMinimumSize(new java.awt.Dimension(111, 23));
        PlayerOverviewButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PlayerOverviewButtonActionPerformed(evt);
            }
        });

        EditTourneyButton.setText("Wijzigen");
        EditTourneyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EditTourneyButtonActionPerformed(evt);
            }
        });

        CancelEditTourneyButton.setLabel("Annuleren");
        CancelEditTourneyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelEditTourneyButtonActionPerformed(evt);
            }
        });

        LocationList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(LocationList);

        TableCapTourneyField.setToolTipText("vanaf 2 tot en met 8");

        jLabel3.setText("Datum:");

        EntryFeeTourneyField.setToolTipText("boven de 0");

        jLabel4.setText("Inleggeld:");

        jLabel5.setText("Maximaal aantal deelnemers:");

        jLabel6.setText("Aantal spelers per tafel:");

        jLabel2.setText("Naam:");

        MaxPlayersTourneyField.setToolTipText("2 of hoger");

        jLabel7.setText("Nieuwe locatie:");

        DateTourneyField.setToolTipText("DD/MM/YYYY");

        jLabel8.setText("Huidige locatie:");

        lblCurrentLocation.setText("Locatie");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addComponent(LocationEventMainButton, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(PlayerOverviewButton, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(CurrentTourneyButton, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 67, Short.MAX_VALUE))
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel7)
                    .addComponent(jLabel6)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(DateTourneyField)
                            .addComponent(MaxPlayersTourneyField, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(EntryFeeTourneyField, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(TableCapTourneyField, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(NameTourneyField, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addComponent(jLabel8)
                        .addGap(18, 18, 18)
                        .addComponent(lblCurrentLocation))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 362, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(EditTourneyButton)
                .addGap(37, 37, 37)
                .addComponent(CancelEditTourneyButton)
                .addGap(51, 51, 51))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(CurrentTourneyButton, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(PlayerOverviewButton, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(LocationEventMainButton, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(40, 40, 40)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(NameTourneyField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(lblCurrentLocation))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(DateTourneyField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(MaxPlayersTourneyField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(EntryFeeTourneyField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(TableCapTourneyField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 102, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CancelEditTourneyButton)
                    .addComponent(EditTourneyButton))
                .addGap(62, 62, 62))
        );

        jLabel7.getAccessibleContext().setAccessibleName("jLabel7");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void CurrentTourneyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CurrentTourneyButtonActionPerformed
        new CurrentTourneyOverview().setVisible(true);
        this.dispose();
    }//GEN-LAST:event_CurrentTourneyButtonActionPerformed

    private void LocationEventMainButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LocationEventMainButtonActionPerformed
        new LocationEventOverview().setVisible(true);
        this.dispose();
    }//GEN-LAST:event_LocationEventMainButtonActionPerformed

    private void PlayerOverviewButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PlayerOverviewButtonActionPerformed
        new PlayerOverview().setVisible(true);
        this.dispose();
    }//GEN-LAST:event_PlayerOverviewButtonActionPerformed

    private void EditTourneyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EditTourneyButtonActionPerformed
        try {

            if (NameTourneyField.getText().equals("") || DateParser.dateStringToMySqlDate(DateTourneyField.getText()) == null) {
                if (DateParser.dateStringToMySqlDate(DateTourneyField.getText()) == null) {
                    JOptionPane.showMessageDialog(null, "Vul de datum in als DD-MM-YYYY");
                } else {
                    JOptionPane.showMessageDialog(null, "Vul alle velden in");
                }

            } else {
                if (LocationList.getSelectedValue() == null) {

                    JOptionPane.showMessageDialog(null, "Geen Locatie geselecteerd");
                } else {


                    anEvent.setName(NameTourneyField.getText());
                    anEvent.setDate(DateParser.dateStringToMySqlDate(DateTourneyField.getText()));
                    anEvent.setLocation_id(getNewLocation());

                    anEvent.setMax_players(Integer.parseInt(MaxPlayersTourneyField.getText()));
                    anEvent.setEntry_fee(Integer.parseInt(EntryFeeTourneyField.getText()));
                    anEvent.setTable_cap(Integer.parseInt(TableCapTourneyField.getText()));
                    if (anEvent.getTable_cap() >= 2 && anEvent.getTable_cap() <= 8 && anEvent.getMax_players() >= 2 && anEvent.getEntry_fee() > 0) {

                        ec.updateTourney(anEvent);

                        new TourneyOverview().setVisible(true);
                        this.dispose();
                    } else {
                        JOptionPane.showMessageDialog(null, "Table cap moet tussen 2 en 8 liggen, "
                                + " Max spelers minimaal 2, inschrijfgeld boven de 0.");
                    }
                }

            }
        } catch (NumberFormatException e) {

            JOptionPane.showMessageDialog(null, "Moeten hele getallen zijn");
        }
    }//GEN-LAST:event_EditTourneyButtonActionPerformed

    private void CancelEditTourneyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelEditTourneyButtonActionPerformed
        new TourneyOverview().setVisible(true);
        this.dispose();
    }//GEN-LAST:event_CancelEditTourneyButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(EditTourney.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(EditTourney.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(EditTourney.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(EditTourney.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new EditTourney().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CancelEditTourneyButton;
    private javax.swing.JButton CurrentTourneyButton;
    private javax.swing.JTextField DateTourneyField;
    private javax.swing.JButton EditTourneyButton;
    private javax.swing.JTextField EntryFeeTourneyField;
    private javax.swing.JButton LocationEventMainButton;
    private javax.swing.JList LocationList;
    private javax.swing.JTextField MaxPlayersTourneyField;
    private javax.swing.JTextField NameTourneyField;
    private javax.swing.JButton PlayerOverviewButton;
    private javax.swing.JTextField TableCapTourneyField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblCurrentLocation;
    // End of variables declaration//GEN-END:variables
}
